﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package net.wooga.pets.views {
    import flash.events.*;
    import flash.display.*;
    import flash.geom.*;
    import net.wooga.pets.loading.vos.*;
    import net.wooga.pets.events.*;
    import net.wooga.pets.models.vo.*;
    import net.wooga.pets.models.*;
    import net.wooga.pets.interfaces.*;
    import net.wooga.pets.views.components.*;
    import net.wooga.pets.constants.*;
    import org.robotlegs.mvcs.*;
    import net.wooga.pets.utils.*;
    import net.wooga.pets.controllers.startup.*;
    import net.wooga.pets.models.world.*;
    import com.adobe.utils.*;
    import flash.ui.*;

    public class CustomizeViewMediator extends Mediator {

		[Inject]
        public var customizeView:CustomizeView;
		[Inject]
        public var roomSlotsModel:RoomSlotsModel;
		[Inject]
        public var sessionModel:SessionModel;
		[Inject]
        public var gameStateModel:GameStateModel;
		[Inject]
        public var userModel:UserModel;
		[Inject]
        public var worldModel:WorldModel;
		[Inject]
        public var gameViewModel:GameViewModel;
        private var _destination:String;
        private var _itemId:String;
        private var _itemType:String;
        private var _slotId:int;
        private var _roomId:String;
        private var _elementVO:ElementVO;
        private var _roomVO:ICustomizable;
        private var _draggedFromInventory:Boolean;
        private var _cancelAllowed:Boolean;// = true

        override public function onRegister():void{
            eventMap.mapListener(this.customizeView, EventConstants.CANCEL, this.onCancel);
            eventMap.mapListener(eventDispatcher, EventConstants.PUT_CUSTOMIZE_ITEM_BACK, this.onPutBack);
            eventMap.mapListener(eventDispatcher, EventConstants.SNAP_IN, this.onSnapIn);
            eventMap.mapListener(eventDispatcher, EventConstants.SNAP_OUT, this.onSnapOut);
            eventMap.mapListener(eventDispatcher, CustomizeEvent.ITEM_CLICKED, this.onItemClicked);
            eventMap.mapListener(eventDispatcher, KeyboardInputEvent.ON_KEYS_PRESSED, this.onKeysPressed);
            eventMap.mapListener(eventDispatcher, ElementEvent.MOVE_ELEMENT, this.onMoveElement);
            eventMap.mapListener(eventDispatcher, EventConstants.SET_BUILD_CANCELATION, this.setCancel);
            eventMap.mapListener(eventDispatcher, KeyboardInputEvent.ON_CHEAT_MODE, this.onCheatMode);
        }
        private function onCancel(_arg1:Event):void{
            if (this._cancelAllowed){
                if (this._draggedFromInventory){
                    this.putBack();
                } else {
                    this.cancel();
                };
            } else {
                dispatch(new InfoEvent(EventConstants.TRY_TO_CANCEL_BUILDMODE, null));
            };
        }
        private function setCancel(_arg1:InfoEvent):void{
            this._cancelAllowed = Boolean(_arg1.info);
        }
        private function onPutBack(_arg1:Event):void{
            if (this._cancelAllowed){
                this.putBack();
            } else {
                dispatch(new InfoEvent(EventConstants.TRY_TO_CANCEL_BUILDMODE, null));
            };
        }
        private function putBack():void{
            var _local1:HandleElementEvent;
            if (this._itemId){
                _local1 = new HandleElementEvent(HandleElementEvent.REMOVE_ELEMENT, this._itemId, this._itemType);
                dispatch(_local1);
            };
            this.cleanUp();
        }
        private function onSnapIn(_arg1:SnapEvent):void{
            this._slotId = _arg1.slotId;
            this._roomVO = (this.worldModel.getRoomById(_arg1.id) as ICustomizable);
            if (this._roomVO){
                this._roomId = ((this._roomId) || (this._roomVO.id));
            };
            var _local2:ISnapArea = _arg1.area;
            var _local3:Rectangle = _local2.getDestinationRect(this._destination);
            this.customizeView.snapInThumb(_local3, this.gameViewModel.hospitalViewScale);
        }
        private function onSnapOut(_arg1:Event=null):void{
            this._slotId = 0;
            this._roomVO = null;
            this.customizeView.snapOutThumb();
        }
        private function onMoveElement(_arg1:ElementEvent):void{
            l("MOVE", _arg1);
            this.gameViewModel.setZoomingEnabled(false);
            this._draggedFromInventory = false;
            this.draggingItem = true;
            var _local2:ICustomizable = (this.worldModel.getRoomById(_arg1.roomId) as ICustomizable);
            var _local3:ElementDisplay = _arg1.element;
            this._roomId = null;
            this._elementVO = _local2.removeElement(_local3.name);
            this._destination = this._elementVO.destination;
            this._itemId = this._elementVO.id;
            this._itemType = this._elementVO.type;
            this.customizeItem(_local3.asset, this.gameViewModel.hospitalViewScale, this.updateItem);
            this.onSnapOut();
        }
        private function onItemClicked(_arg1:CustomizeEvent):void{
            this.gameViewModel.setZoomingEnabled(false);
            this._draggedFromInventory = true;
            this.draggingItem = true;
            this._destination = _arg1.destination;
            this._itemId = _arg1.id;
            this._itemType = _arg1.itemType;
            switch (this._destination){
                case ShopItemVO.DESTINATION_ROOM:
                    this.customizeRoom();
                    break;
                case ShopItemVO.DESTINATION_FACADE:
                case ShopItemVO.DESTINATION_WALL:
                case ShopItemVO.DESTINATION_FLOOR:
                case ShopItemVO.DESTINATION_CEILING:
                    this.onCustomizeItem();
                    break;
                default:
                    l("e Unknown category:", _arg1.destination);
            };
        }
        private function set draggingItem(_arg1:Boolean):void{
            var _local2:String = (_arg1) ? EventConstants.ON_START_DRAGGING_CUSTOMIZE_ITEM : EventConstants.ON_STOP_DRAGGING_CUSTOMIZE_ITEM;
            dispatch(new Event(_local2));
        }
        private function onKeysPressed(_arg1:KeyboardInputEvent):void{
            if (ArrayUtil.arraysAreEqual(_arg1.keys, [Keyboard.ESCAPE])){
                l("ESCAPE");
                this.cancel();
            };
        }
        private function onCustomizeItem():void{
            var _local2:Function;
            var _local1:MovieClip = AssetsLibrary.getMovieClip(this._itemType);
            if (_local1){
                _local2 = (this._itemId) ? this.updateItem : this.createItem;
                this.customizeItem(_local1, CustomizeView.DEFAULT_SCALE, _local2);
            } else {
                l(("e No Clip named " + this._itemType));
            };
        }
        private function customizeItem(_arg1:MovieClip, _arg2:Number, _arg3:Function):void{
            eventMap.unmapListener(eventDispatcher, CustomizeEvent.ITEM_CLICKED, this.onItemClicked);
            this.customizeView.customize(_arg1, _arg2);
            this.customizeView.addEventListener(CustomizeViewEvent.PLACE_ITEM, _arg3);
        }
        private function customizeRoom():void{
            var _local1:RequestEvent;
            eventMap.unmapListener(eventDispatcher, CustomizeEvent.ITEM_CLICKED, this.onItemClicked);
            if (AssetsLibrary.getMovieClip(this._itemType)){
                this.initCustomizeView(this._itemType);
            } else {
                _local1 = new RequestEvent(EventConstants.SEND_REQUEST, this.onRoomLoaded);
                _local1.addAssetRequest(ProtocolConstants.LOAD_ROOM, this._itemType.toLowerCase());
                dispatch(_local1);
                this.initCustomizeView(GameConstants.ROOM_DUMMY);
            };
        }
        private function onRoomLoaded(_arg1:ResultVO):void{
            var _local2:MovieClip;
            if (_arg1.success){
                _local2 = AssetsLibrary.getMovieClip(this._itemType);
                l("loaded room:", this._itemType, _local2);
            } else {
                l("could not load room:", this._itemType);
            };
        }
        private function initCustomizeView(_arg1:String):void{
            dispatch(new Event(EventConstants.DEACTIVATE_ITEMS));
            var _local2:MovieClip = AssetsLibrary.getMovieClip(_arg1);
            this.customizeView.customize(_local2, CustomizeView.DEFAULT_SCALE);
            this.customizeView.addEventListener(CustomizeViewEvent.PLACE_ITEM, this.placeRoom);
        }
        private function createItem(_arg1:CustomizeViewEvent):void{
            if (this.userModel.coins > 0){
                this.callItemService(_arg1, HandleElementEvent.CREATE_ELEMENT);
            } else {
                this.cancel();
            };
        }
        private function updateItem(_arg1:CustomizeViewEvent):void{
            this.callItemService(_arg1, HandleElementEvent.UPDATE_ELEMENT);
        }
        private function callItemService(_arg1:CustomizeViewEvent, _arg2:String):void{
            var _local3:HandleElementEvent;
            if (this._roomVO){
                _local3 = new HandleElementEvent(_arg2, this._itemId, this._itemType);
                _local3.x = (_arg1.data.x / this.gameViewModel.hospitalViewScale);
                _local3.y = (_arg1.data.y / this.gameViewModel.hospitalViewScale);
                _local3.z = this._roomVO.nextMaxZ;
                _local3.roomId = this._roomVO.id;
                dispatch(_local3);
                this.cleanUp();
            } else {
                this.cancel();
            };
        }
        private function placeRoom(_arg1:CustomizeViewEvent):void{
            if (this._slotId){
                dispatch(new InfoEvent(EventConstants.SET_BUILD_CANCELATION, null, true));
                dispatch(new InfoEvent(EventConstants.LOAD_ROOM, null, {room_type:this._itemType, slot:this._slotId}));
                if (this._itemType == GameConstants.TYPE_FOUNTAIN){
                    dispatch(new TrackingEvent(TrackingConst.TRACK, TrackingConst.TUTORIAL, {step:"f10_fountain_placed"}));
                };
            };
            this.cleanUp();
        }
        private function cancel():void{
            var _local1:HandleElementEvent;
            if (this._cancelAllowed){
                if (this._elementVO){
                    _local1 = new HandleElementEvent(HandleElementEvent.UPDATE_ELEMENT, this._elementVO.id, this._itemType);
                    _local1.x = this._elementVO.x;
                    _local1.y = this._elementVO.y;
                    _local1.roomId = this._roomId;
                    dispatch(_local1);
                };
                this.cleanUp();
            } else {
                dispatch(new InfoEvent(EventConstants.TRY_TO_CANCEL_BUILDMODE, null));
            };
        }
        private function cleanUp():void{
            this.gameViewModel.setZoomingEnabled(true);
            dispatch(new Event(EventConstants.ACTIVATE_ITEMS));
            this._slotId = 0;
            this._roomVO = null;
            this._roomId = null;
            this._elementVO = null;
            this._itemId = null;
            this.customizeView.removeEventListener(CustomizeViewEvent.PLACE_ITEM, this.placeRoom);
            this.customizeView.removeEventListener(CustomizeViewEvent.PLACE_ITEM, this.createItem);
            this.customizeView.removeEventListener(CustomizeViewEvent.PLACE_ITEM, this.updateItem);
            eventMap.mapListener(eventDispatcher, CustomizeEvent.ITEM_CLICKED, this.onItemClicked);
            this.customizeView.cancel();
            dispatch(new Event(EventConstants.REMOVE_SNAPAREAS));
            this.draggingItem = false;
        }
        private function onCheatMode(_arg1:Event):void{
            this._cancelAllowed = true;
        }

    }
}//package net.wooga.pets.views 
